package com.sgb.capital.view.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.sgb.capital.R;
import com.sgb.capital.base.BaseActivity;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.callback.ZMessageEvent;
import com.sgb.capital.databinding.TransferaccountsdetailsActivityBinding;
import com.sgb.capital.model.Bean;
import com.sgb.capital.model.PayDetailBean;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.view.pop.TipPop;
import com.sgb.capital.view.ui.adapter.BalanceDetailsAdapter;
import com.sgb.capital.view.widget.MToast;
import com.sgb.capital.viewmodel.DetailsModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

/**
 * 作者:张磊
 * 日期:2021/10/11 0011
 * 说明:转账详情
 */
public class TransferAccountsDetailsActivity extends BaseActivity {
    private TransferaccountsdetailsActivityBinding mBinding;
    private DetailsModel mModel;
    private BalanceDetailsAdapter mAdapter;
    private TipPop mTipPop;
    private PayDetailBean mDetailBean;
    private int mSwitchOn;

    public static void start(Context context, String id) {
        Intent intent = new Intent(context, TransferAccountsDetailsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }


    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        mBinding = DataBindingUtil.setContentView(this, R.layout.transferaccountsdetails_activity);
        mModel = new ViewModelProvider(this).get(DetailsModel.class);
        mBinding.rv.setLayoutManager(new GridLayoutManager(Utils.getContext(), 1));
        mTipPop = new TipPop(this);
        mAdapter = new BalanceDetailsAdapter(null);
        mBinding.rv.setAdapter(mAdapter);

    }

    public void initData() {
        mModel.getProcessInfo(this);

    }

    public void initListener() {
        mBinding.ivBack.setOnClickListener(v -> finish());


        mBinding.tvBtn.setOnClickListener(v -> {
            mModel.list(this);
        });
    }

    @Override
    public void initObserve() {
        mModel.mProcessInfoBean.observe(this, processInfoBean -> {
            mSwitchOn = processInfoBean.switchOn;
            mModel.getTransferDetails(this, getIntent().getStringExtra("id"));
        });
        mModel.mListBean.observe(this, listBeans -> {
            if (listBeans == null || listBeans.size() == 0) {
                mTipPop.initStr("复核人员为空，请在电脑端-业务管理-复核配置进行相关人员的配置！", null, "知道了");
                mTipPop.show();
            } else {
                PayDealActivity.start(this, mDetailBean.orderId, false, Utils.getDecimalFormat(mDetailBean.transferMoney));
            }
        });
        mModel.mPayDetailBean.observe(this, data -> {
            if (data == null) {
                MToast.showToast(Utils.getContext(), "订单不存在!");
                return;
            }

            if (data.payStatus == 2 && (data.payApplyState == 2 || data.payApplyState == 0)) {
                mBinding.flBg.setVisibility(View.VISIBLE);
            } else {
                mBinding.flBg.setVisibility(View.GONE);
            }
            mDetailBean = data;
            String phone = data.reviewerPhone == null ? "" : data.reviewerPhone.substring(0, 3) + "****" + data.reviewerPhone.substring(data.reviewerPhone.length() - 4);
            List<Bean> beans = new ArrayList<>();
            beans.add(new Bean("交易类型", "转账汇款"));
            beans.add(new Bean("转账结果", data.transferResult == 1 ? "转账完成" : "/"));
            beans.add(new Bean("审批状态", data.payApplyState == 0 ? "/" : data.payApplyState == 1 ? "审批中" : data.payApplyState == 2 ? "已通过" : "已拒绝"));
            beans.add(new Bean("转账单号", data.orderId));
            beans.add(new Bean("转账收款方", data.payeeCompanyName));
            beans.add(new Bean("法人姓名", data.payeeLegalName));
            beans.add(new Bean("统一社会信用代码", data.payeeCertId));
            beans.add(new Bean("企业地址", data.payeeAddress));
            beans.add(new Bean("转账方式",
                    data.method.contains("BALANCE") ? "余额支付" :
                            data.method.contains("WX") ? "微信支付" :
                                    data.method.contains("AL") ? "支付宝支付" : "B2B网银"
            ));
            if (data.method.contains("B2B")) {
                beans.add(new Bean("转账发起时间", Utils.getTime(data.created)));
                beans.add(new Bean("交易金额（元）", Utils.getDecimalFormat(data.transferMoney)));
                beans.add(new Bean("转账手续费（元）", Utils.getDecimalFormat(data.transferFees)));
            } else {
                beans.add(new Bean("付款状态", data.payStatus == 0 ? "未付款" :
                        data.payStatus == 1 ? "冻结中" :
                                data.payStatus == 2 ? "已付款" : "支付失败"));
                beans.add(new Bean("复核人员", Utils.isEmpty(phone) ? data.reviewerName : data.reviewerName + "(" + phone + ")"));
                beans.add(new Bean("转账发起时间", Utils.getTime(data.created)));
                beans.add(new Bean("交易金额（元）", Utils.getDecimalFormat(data.transferMoney)));
            }
            beans.add(new Bean("转账备注", Utils.isEmpty(data.remarks) ? "/" : data.remarks));
            mAdapter.setNewInstance(beans);


            if (data.transferResult != 1 && data.payApplyState == 2 && data.method.contains("BALANCE")) {
                mBinding.tvBtn.setText("立即转账");
                mBinding.tvBtn.setBackgroundResource(R.drawable.button);
                mBinding.tvBtn.setTextColor(Utils.getColor(R.color.white));
                mBinding.tvBtn.setVisibility(View.VISIBLE);
                mBinding.tvBtn.setEnabled(true);
            } else if (data.payApplyState == 1 || data.payApplyState == 3) {
                mBinding.tvBtn.setText("请前往PC端查看审批详情");
                mBinding.tvBtn.setBackgroundResource(R.drawable.button_false);
                mBinding.tvBtn.setTextColor(Utils.getColor(R.color.color_FFC000));
                mBinding.tvBtn.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe
    public void getCode(ZMessageEvent event) {
        switch (event.code) {
            case Constants
                    .EVENT_HIDE_ORDERID:
                finish();
                break;
        }
    }
}